<?php
require './vendor/autoload.php';

use JasonGrimes\Paginator;

$totalItems = 300; // 总记录数
$itemsPerPage = 10; // 每页显示数

$currentPage = 1;
if (isset($_GET['page'])) {
    $currentPage = intval($_GET['page']);
}

// $currentPage = 8;
// $urlPattern = '/foo/page/(:num)';
//这里换上你自己的页面链接
$urlPattern = 'http://hollis.test.com/page?page=(:num)';
$paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);

//$info = $paginator->getPages();
//print_r($info);

//1.默认分页，在html页面直接输出$paginator即可
$paginator->setMaxPagesToShow(7);

//2.普通中文+数字分页，在html页面直接输出$html即可
$html = '<ul class="pagination">';
if ($paginator->getPrevUrl()) {
    $html .= '<li><a href="' . $paginator->getPrevUrl() . '">&laquo; 上一页</a></li>';
}

foreach ($paginator->getPages() as $page) {
    if ($page['url']) {
        $html .= '<li' . ($page['isCurrent'] ? ' class="active"' : '') . '><a href="' . $page['url'] . '">' . $page['num'] . '</a></li>';
    } else {
        $html .= '<li class="disabled"><span>' . $page['num'] . '</span></li>';
    }
}

if ($paginator->getNextUrl()) {
    $html .= '<li><a href="' . $paginator->getNextUrl() . '">下一页 &raquo;</a></li>';
}
$html .= '</ul>';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>PHP分页组件Paginator使用实例</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/demo.css">
</head>

<body>
<div class="container">
    <div class="row main" style="margin-top: 50px;">
        <div class="col-md-12">
            <h2 class="top_title">
                PHP分页组件-Paginator
            </h2>

            <div class="row" style="margin-top: 30px">
                <div class="col-md-offset-2 col-sm-8">
                    <p>1.默认分页</p>
                    <?php
                    echo $paginator;
                    ?>
                    <p>2.普通中文+数字分页</p>
                    <?php
                    echo $html;
                    ?>
                    <div style="clear: both"></div>

                </div>
            </div>
        </div>
        <script src="js/jquery.min.js"></script>
        <script>
            $(function () {
                $('.paginator-select-page').on('change', function () {
                    document.location = $(this).val();
                });
                // Workaround to prevent iOS from zooming the page when clicking the select list:
                $('.paginator-select-page')
                    .on('focus', function () {
                        if (/(iPad|iPhone|iPod)/g.test(navigator.userAgent)) {
                            $(this).css('font-size', '16px');
                        }
                    })
                    .on('blur', function () {
                        if (/(iPad|iPhone|iPod)/g.test(navigator.userAgent)) {
                            $(this).css('font-size', '');
                        }
                    })
                ;
            });
        </script>
</body>
</html>